/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.account.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.account.api.entity.MemberRegisterForm;
import com.medusa.gruul.account.mapper.MemberRegisterFormMapper;
import com.medusa.gruul.account.model.vo.MemberRegisterFormVo;
import com.medusa.gruul.account.service.IMemberRegisterFormService;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 会员注册表单
 * @Author whh
 * @Date 2021-1-17
 **/
@Service
public class MemberRegisterFormServiceImpl extends ServiceImpl<MemberRegisterFormMapper, MemberRegisterForm> implements IMemberRegisterFormService {


    @Override
    public void operationMemberRegisterForm(List<MemberRegisterFormVo> memberRegisterFormVos) {
        if (CollectionUtil.isEmpty(memberRegisterFormVos)) {
            return;
        }
        List<Long> exist = new ArrayList<>(memberRegisterFormVos.size());
        //会员注册表单信息
        for (MemberRegisterFormVo memberRegisterFormVo : memberRegisterFormVos) {
            MemberRegisterForm memberRegisterForm = this.baseMapper.qryFormById(memberRegisterFormVo.getFormId(), TenantContextHolder.getTenantId());
            //更新
            if (memberRegisterForm == null) {
                memberRegisterForm = new MemberRegisterForm();
            }
            memberRegisterForm.setFormName(memberRegisterFormVo.getFormName())
                    .setFormPattern(memberRegisterFormVo.getFormPattern())
                    .setIsRequired(memberRegisterFormVo.getIsRequired())
                    .setTips(memberRegisterFormVo.getTips())
                    .setFormId(memberRegisterFormVo.getFormId());
            this.saveOrUpdate(memberRegisterForm);
            exist.add(memberRegisterForm.getId());
        }
        List<MemberRegisterForm> registerForms = this.baseMapper.selectList(new QueryWrapper<MemberRegisterForm>().notIn("id", exist));
        if (CollectionUtil.isNotEmpty(registerForms)) {
            this.baseMapper.deleteBatchIds(registerForms.stream().map(MemberRegisterForm::getId).collect(Collectors.toList()));
        }
    }
}
